<?php
class mem_db {
    public $mem_obj;
    public $expire = 3600;
    public function __construct($mem_cfg) {
        $this->mem_obj = new Memcached();
        $re = $this->mem_obj->addServer($mem_cfg['host'],$mem_cfg['port']);
        if($re == false) {
            die('Memcached Server Add Error');
        }
        $this->expire = $mem_cfg['expire'];
    }
    public function set($key,$value, $expire =null) {
        if($expire) $this->expire = $expire;
        $current = $this->get($key);
        if($current) {
            return $this->mem_obj->set($key,$value,$this->expire);
        } else {
            return $this->mem_obj->add($key,$value,$this->expire);
        }
    }
    public function get($key) {
        return $this->mem_obj->get($key);
    }
    public function del($key) {
        $re = $this->mem_obj->delete($key);
        return $re;
    }
}

